package com.myweather.android.fragment;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.google.gson.Gson;
import com.myweather.android.R;
import com.myweather.android.adapter.TouTiaoAdapter;
import com.myweather.android.bean.Toutiao;
import com.myweather.android.util.HttpUtil;


import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

/**
 * @author zcq
 * @package com.myweather.android.fragment
 * @fileName myweather-master
 * @description: 这个类的描述
 * @org 融信通达科技有限公司
 * @email @163.com
 * @date 2018/11/29  10:27
 */
public class FragmentTab1 extends Fragment {
    private View view;
    private RecyclerView mRc1;
    private List<Toutiao.ResultBean.DataBean> mDataBeanList = new ArrayList<>();
    private TouTiaoAdapter touTiaoAdapter;
    private String url = "http://v.juhe.cn/toutiao/index?type=top&key=52a4b8a337f42dd376661f8b61c7ba6d";

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_fg_tab1, null);

        initView(view);
        initData(url);
        return view;
    }


    private void initData(String url) {
        HttpUtil.sendOkHttpRequset(url, new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String body = response.body().string();

                Gson gson = new Gson();
                Toutiao toutiao = gson.fromJson(body, Toutiao.class);
                if(toutiao!=null){
                    if(!"成功的返回".equals(toutiao.getReason())){
                        return;

                    }
                    mDataBeanList.clear();
                    mDataBeanList.addAll(toutiao.getResult().getData());
                    getActivity().runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            touTiaoAdapter.notifyDataSetChanged();
                        }
                    });
                }



            }
        });
    }

    public void upData(String url) {
        initData(url);

    }

    private void initView(View view) {
        mRc1 = (RecyclerView) view.findViewById(R.id.rc_1);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
        mRc1.setLayoutManager(linearLayoutManager);
        touTiaoAdapter = new TouTiaoAdapter(getActivity(), mDataBeanList);
        mRc1.setAdapter(touTiaoAdapter);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }


}
